package com.qw.soul.permission.request.fragment;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;

import androidx.annotation.NonNull;

import com.qw.soul.permission.PermissionRequestCode;
import com.qw.soul.permission.PermissionTools;
import com.qw.soul.permission.bean.Permission;
import com.qw.soul.permission.bean.Special;
import com.qw.soul.permission.callbcak.RequestPermissionListener;
import com.qw.soul.permission.callbcak.SpecialPermissionListener;
import com.qw.soul.permission.checker.SpecialChecker;
import com.qw.soul.permission.debug.PermissionDebug;
import com.qw.soul.permission.request.IPermissionActions;

import static android.os.Build.VERSION_CODES.M;

/**
 * @author cd5160866
 */
public class PermissionFragment extends Fragment implements IPermissionActions
{

    private static final String TAG = PermissionFragment.class.getSimpleName();

    private Special specialToRequest;

    private RequestPermissionListener runtimeListener;

    private SpecialPermissionListener specialListener;

    @TargetApi(M)
    @Override
    public void requestPermissions(String[] permissions, RequestPermissionListener listener)
    {
        this.runtimeListener = listener;
        requestPermissions(permissions, PermissionRequestCode.REQUEST_CODE_PERMISSION);
    }

    @Override
    public void requestSpecialPermission(Special permission, SpecialPermissionListener listener)
    {
        this.specialListener = listener;
        this.specialToRequest = permission;
        Intent intent = PermissionTools.getSpecialPermissionIntent(getActivity(), specialToRequest);
        if (null == intent)
        {
            PermissionDebug.w(TAG, "create intent failed");
            return;
        }
        try
        {
            startActivityForResult(intent, PermissionRequestCode.REQUEST_CODE_PERMISSION_SPECIAL);
        } catch (Exception e)
        {
            e.printStackTrace();
            PermissionDebug.e(TAG, e.toString());
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        Activity activity = getActivity();
        if (null == specialListener || !PermissionTools.isActivityAvailable(activity))
        {
            return;
        }
        if (requestCode == PermissionRequestCode.REQUEST_CODE_PERMISSION_SPECIAL)
        {
            boolean result = new SpecialChecker(activity, specialToRequest).check();
            if (result)
            {
                specialListener.onGranted(specialToRequest);
            } else
            {
                specialListener.onDenied(specialToRequest);
            }
        }
    }

    @TargetApi(M)
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
    {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        Permission[] permissionResults = new Permission[permissions.length];
        if (requestCode == PermissionRequestCode.REQUEST_CODE_PERMISSION)
        {
            for (int i = 0; i < permissions.length; ++i)
            {
                Permission permission = new Permission(permissions[i], grantResults[i], this.shouldShowRequestPermissionRationale(permissions[i]));
                permissionResults[i] = permission;
            }
        }
        if (null != runtimeListener && PermissionTools.isActivityAvailable(getActivity()))
        {
            runtimeListener.onPermissionResult(permissionResults);
        }
    }
}
